package util;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;


public class FontImage {

	 public static void main(String[] args)  
	    {  
		 	String nickname = "1";
		 	String path = "D://2.png";
		 	
	        createImage(nickname, path);  
	    }

	public static void createImage(String nickname, String path) {
		BufferedImage imgMap = drawTranslucentStringPic(150, 16, 12, nickname);  
		File imgFile=new File(path);  
		try  
		{  
		    ImageIO.write(imgMap, "PNG", imgFile);  
		} catch (IOException e)  
		{  
		    e.printStackTrace();  
		}  
		System.out.println("生成完成");
	}
	
    //生成无背景文字图片
    public static BufferedImage drawTranslucentStringPic(int width, int height, Integer fontHeight, String drawStr)  
    {  
        try  
        {  
            BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);  
            Graphics2D gd = buffImg.createGraphics();  
            //设置透明  start  
            buffImg = gd.getDeviceConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT);  
            gd=buffImg.createGraphics();  
            //设置透明  end  
            gd.setFont(new Font("微软雅黑", Font.PLAIN, fontHeight)); //设置字体  
        //    gd.setColor(Color.white); 
            gd.setColor(new Color(255,255,255,128));  //设置颜色  
            gd.drawString(drawStr, fontHeight, fontHeight); //输出文字（中文横向居中）  
            return buffImg;  
        } catch (Exception e) {  
            return null;  
        }  
    } 
}
